package com.changgou.user.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.user.config.TokenDecode;
import com.changgou.user.pojo.Collect;
import com.changgou.user.service.CollectService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private TokenDecode tokenDecode;

    @Autowired
    private SkuFeign skuFeign;

    @GetMapping("/save/{id}/{username}")
    @ResponseBody
    public int save(@PathVariable("id") String id, @PathVariable("username") String username){
        try {
            int save = collectService.save(id, username);

            return save;

        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

    }
    @RequestMapping("/usernameBySkuId")
    @ResponseBody
    public Result usernameBySkuId(){
        String username = tokenDecode.getUserInfo().get("username");
        List<Collect> list= collectService.findBySkuId(username);
        List<Sku> skus = new ArrayList<>();
        for (Collect collect : list) {
            Result<Sku> byId = skuFeign.findById(collect.getSkuId());
            skus.add(byId.getData());
        }
        Map map = new HashMap();
        map.put("list",skus);
        return new Result(true,StatusCode.OK,"dsfs",map);
    }

}
